﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using MongoDB.Driver;

namespace Dark_Core_Library.DatabaseModules.MongoDB
{
    /// <summary>
    /// MongoDB帮助类
    /// </summary>
    public static class MongoDBHelper
    {
        /// <summary>
        /// 创建一个MongoDB的集合
        /// </summary>
        /// <typeparam name="TDocument">集合中文档的类型</typeparam>
        /// <param name="databaseName">数据库名称</param>
        /// <param name="collectionName">集合名称</param>
        /// <param name="userName">用户名</param>
        /// <param name="password">密码</param>
        /// <param name="remoteHost">远程主机</param>
        /// <param name="port">端口号</param>
        /// <returns></returns>
        public static IMongoCollection<TDocument> CreateCollection<TDocument>(
            string databaseName,
            string collectionName,
            string remoteHost,
            string? userName = null,
            string? password = null,
            string? args = null,
            int? port = 27017
        )
        {
            //(isAnonymous == true ? "" : )
            var url =
                "mongodb://" + ((userName == null) || (password == null) || (userName == "") || (password == "") ? "" : (userName + ":" + password + "@")) + remoteHost + ":" + port + "/" + args ?? "";
            var client = new MongoClient(url);
            var database = client.GetDatabase(databaseName);
            var collection = database.GetCollection<TDocument>(collectionName);
            return collection;
        }
    }
}
